var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '123456',
  database : 'test'
});
connection.connect();
connection.query('SELECT * FROM user_info', function(err, rows, fields) {
  if (err) throw err;
  console.log('The rows[0] is: ', rows[0]);
  //console.log('fields :',fields);
});
connection.end();

//使用连接池
var pool = mysql.createPool({
	connectionLimit : 10,
	host			:"localhost",
	user 			:"root",
	password		:"123456",
	database		:"test"
});

pool.getConnection(function(err,conn){
  if(err){
    console.log(err);
    return;
  }
  conn.query("SELECT * FROM user_info",function(err,rows){

    console.log(rows);

    conn.release();

    //pool.end();
  });
});

//获取连接池的链接
pool.getConnection(function(err,conn){
  if(err){
    console.log(err);
    return;
  }
  conn.query("insert into user_info set ?",{name:"name123"},function(err,result){
    
    console.log(result.insertId);

    conn.query("SELECT * FROM user_info",function(err,rows){

      console.log(rows);

      //conn.release();

      //pool.end();
    });

    //删除
    conn.query("DELETE FROM user_info WHERE name = 'name123'",function(err,delRet){
      console.log("delRet.affectedRows:");
      console.log(delRet.affectedRows);

      //conn.release();

      //pool.end();
    });    
  });

  //更新文件
  conn.query("insert into user_info set ?",{name:"name12345"},function(err,insertRet){
    
    console.log(insertRet.insertId);

    conn.query("SELECT * FROM user_info",function(err,rows){

      console.log(rows);

      //conn.release();
      //pool.end();
    });

    //删除
    conn.query("UPDATE user_info SET name = 'name_changed' WHERE name = 'name12345'",
      [insertRet.insertId],
      function(err,updateRet){
      console.log("updateRet.changedRows:");
      console.log(updateRet.changedRows);

      //conn.release();
      //pool.end();
    });    
  });

});
